package com.example.shushiyunsst.fragment.main.home.child.new_sale.chlid

import android.content.Context
import android.util.Log
import android.widget.ImageView
import android.widget.TextView
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.alibaba.fastjson.JSON
import com.example.library_common.adapter.JssBaseQuickAdapter
import com.example.library_common.adapter.JssBaseViewHolder
import com.example.library_common.album.view.grid_view.XFlowLayout
import com.example.library_common.album.view.grid_view.adapter.CommAdapter
import com.example.library_common.album.view.grid_view.adapter.XQuickViewHolder
import com.example.library_common.glidmodule.GlideApp
import com.example.library_common.utils.PixUtils
import com.example.shushiyunsst.R
import com.example.shushiyunsst.fragment.main.home.child.new_sale.chlid.bean.GoodTypeBean
import com.example.shushiyunsst.fragment.main.home.child.new_sale.chlid.bean.GoodTypeSubBean
import com.example.shushiyunsst.fragment.main.home.child.new_sale.chlid.bean.SecKillSubBean
import com.lxj.xpopup.core.BottomPopupView
import com.lxj.xpopup.util.XPopupUtils

class SecKillGoodTypePop(context: Context) : BottomPopupView(context) {

    var mBean: SecKillSubBean? = null

    private lateinit var infoDaoIcon: ImageView

    private var goodsName: TextView? = null

    private var goodsPrice: TextView? = null

    private var oriPrice: TextView? = null

    private var goodsStore: TextView? = null

    private var mRecyclerView: RecyclerView? = null

    private var mAdapter: JssBaseQuickAdapter<GoodTypeBean>? = null

    override fun getImplLayoutId(): Int {
        return R.layout.sec_kill_good_type_pop
    }

    override fun onCreate() {
        super.onCreate()
        infoDaoIcon = findViewById(R.id.infoDaoIcon)
        goodsName = findViewById(R.id.goodsName)
        goodsPrice = findViewById(R.id.goodsPrice)
        oriPrice = findViewById(R.id.oriPrice)
        goodsStore = findViewById(R.id.goodsStore)
        mRecyclerView = findViewById(R.id.mRecyclerView)

        GlideApp.with(context)
            .load(mBean?.goodsLogo)
            .centerCrop()
            .into(infoDaoIcon)

        goodsName?.text = mBean?.goodsName
        goodsPrice?.text = mBean?.skpPriceStr
        oriPrice?.text = mBean?.skpPriceOldStr
        goodsStore?.text = mBean?.skpNumsStr

        mRecyclerView?.setHasFixedSize(true)
        mRecyclerView?.layoutManager =
            LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false)
        mAdapter =
            object : JssBaseQuickAdapter<GoodTypeBean>(R.layout.sec_kill_good_type_item_pop) {
                override fun convert(holder: JssBaseViewHolder?, item: GoodTypeBean?) {
                    super.convert(holder, item)
                    holder?.setText(R.id.mType, item?.typeName)
                    val mGird: XFlowLayout? = holder?.findView22(R.id.mGird)
                    val mCommAdapter = object :
                        CommAdapter<GoodTypeSubBean>(
                            context,
                            R.layout.sec_kill_good_type_item_sub_pop
                        ) {
                        override fun convert(
                            holder: XQuickViewHolder?,
                            subItem: GoodTypeSubBean?,
                            position: Int
                        ) {
                            holder?.setText(R.id.mRTextView, subItem?.name)
                                ?.setSelect(
                                    R.id.mRTextView,
                                    subItem?.isEquals(item?.selectedItem) == true
                                )
                                ?.setOnClickListener {
                                    item?.selectedItem = subItem
                                    notifyDataSetChanged()
                                }
                        }

                    }
                    mGird?.adapter = mCommAdapter
                    mCommAdapter.clear()
                    mCommAdapter.addAll(item?.subList)
                }
            }

        mRecyclerView?.adapter = mAdapter

        try {
            val mColorBean = GoodTypeBean()
            mColorBean.typeName ="颜色"
            mColorBean.subList=parseData(
                "color",
                "{\"color\":[{\"name\":\"大红色\"},{\"name\":\"绿色\"}]}"
            )
            mAdapter?.addData(mColorBean)

            val mSizeBean = GoodTypeBean()
            mSizeBean.typeName ="尺寸"
            mSizeBean.subList=parseData(
                "size",
                "{\"size\":[{\"name\":\"XL\"},{\"name\":\"XLLL\"}]}"
            )
            Log.d("onCreate", "onCreate: ${mSizeBean.subList}")
            mAdapter?.addData(mSizeBean)
            mAdapter?.notifyDataSetChanged()
            doShowAnimation()
        } catch (e: Exception) {
            e.printStackTrace()
        }
        targetSizeView.measure(0,0)
        XPopupUtils.setWidthHeight(
            targetSizeView,
            if (maxWidth != 0 && popupWidth > maxWidth) maxWidth else popupWidth,
            popupHeight
        )
    }

//    override fun onShow() {
//        super.onShow()
//        popupContentView.postDelayed({doShowAnimation()}, 300)
//    }

    override fun getPopupHeight(): Int {
        targetSizeView.measure(0,0)
        return  targetSizeView?.measuredHeight?:maxHeight
    }

    private val doAfterShowTask = Runnable {

        //再次检测移动距离
        if (dialog == null) return@Runnable
        if (XPopupUtils.getDecorViewInvisibleHeight(dialog.window) > 0) {
            XPopupUtils.moveUpToKeyboard(
                XPopupUtils.getDecorViewInvisibleHeight(dialog.window),
                this
            )
        }
    }

    private fun parseData(parseKey: String, jsonStr: String): List<GoodTypeSubBean> {

        val obj = JSON.parseObject(jsonStr)
        val value = obj[parseKey].toString()
        var array = JSON.parseArray(value, GoodTypeSubBean::class.java)
        if (array == null) array = ArrayList<GoodTypeSubBean>()
        return array
    }

}